package com.jiang.service.impl;

import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.jiang.dao.INewsDao;
import com.jiang.service.INewsService;
import com.jiang.vo.News;

@Service
public class NewsServiceImpl implements INewsService {

	@Autowired
	private INewsDao newsDao;

	@Override
	public boolean insert(News vo) {

		try {
			return newsDao.doCreate(vo);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return false;
	}

	@Override
	public boolean update(News vo) {

		try {
			return newsDao.doUpdate(vo);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return false;
	}

	@Override
	public boolean delete(Set<Integer> ids) {
		try {
			return newsDao.doRemoveBatch(ids);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return false;
	}

	@Override
	public News get(int id) {
		try {
			return newsDao.findById(id);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return null;
	}

	@Override
	public List<News> list() {

		try {
			return newsDao.findAll();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return null;
	}

	@Override
	public Map<String, Object> list(String column, String keyWord, int currentPage, int lineSize) {

		Map<String, Object> map = null;
		try {
			map = new HashMap<>(5);
			List<News> resultData = newsDao.findAllSplit(column, keyWord, currentPage, lineSize);
			Long count = newsDao.getAllCount(column, keyWord);
			map.put("data", resultData);
			map.put("count", count);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return map;
	}

}
